package io.dshubs.archives.controller;


import io.dshubs.archives.ArchivesApplication;
import io.dshubs.archives.dao.ArchiveAppraisalMapper;
import io.dshubs.archives.entity.ArchiveAppraisal;
import io.dshubs.archives.entity.ArchiveBaseInfo;
import io.dshubs.archives.service.ArchiveAppraisalService;
import io.dshubs.archives.service.ArchiveBaseInfoService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案鉴定表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-27
 */
@Api(tags = "档案鉴定表")
@RestController
@RequestMapping("/appraisal")
public class ArchiveAppraisalController {

    @Autowired
    private ArchiveAppraisalService archiveAppraisalService;

    @Autowired
    private ArchiveAppraisalMapper archiveAppraisalMapper;

    @GetMapping("page")
    @ApiOperation("page")
    @LogOperation("档案鉴定:/archive/appraisal:/img/icon-档案鉴定.png:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "appraisalTitle", value = "题名", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "fileNumber", value = "档号", paramType = "query", dataType="String")
    })

    public Result<PageData<ArchiveAppraisal>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveAppraisal>>().ok(archiveAppraisalService.page(params));
    }

    @GetMapping("{id}")
    @ApiOperation("详情")
    @LogOperation("档案鉴定:archive/appraisal:/img/icon-档案鉴定.png:get")
    public Result<ArchiveAppraisal> get(@PathVariable("id") Long id){
        return new Result<ArchiveAppraisal>().ok(archiveAppraisalService.selectById(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("档案鉴定:save")
    public Result save(@RequestBody ArchiveAppraisal archiveAppraisal){
        //效验数据
        ValidatorUtils.validateEntity(archiveAppraisal, AddGroup.class, DefaultGroup.class);
        List<String> fileNumberList = archiveAppraisal.getFileNumberList();
        if(CollectionUtils.isNotEmpty(fileNumberList)) {
            fileNumberList.forEach(item->{
                archiveAppraisal.setFileNumber(item);
                archiveAppraisal.setAppraisalDate(new Date());
                archiveAppraisalService.insert(archiveAppraisal);
            });
        }else {
            throw new RenException("未选择鉴定文件");
        }
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("档案鉴定:update")
    public Result update(@RequestBody ArchiveAppraisal archiveAppraisal){
        //效验数据
        ValidatorUtils.validateEntity(archiveAppraisal, UpdateGroup.class, DefaultGroup.class);

        archiveAppraisalService.updateById(archiveAppraisal);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("档案鉴定:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveAppraisalService.deleteBatchIds(ids);
        return new Result();
    }


    @PutMapping("/restore")
    @LogOperation("档案鉴定:restore")
    public Result restore(@RequestBody @NotNull(message = "还原id不能为空") List<String> ids){
        archiveAppraisalService.restore(ids);
        return new Result();
    }


    @PutMapping("/authenticated")
    @LogOperation("档案鉴定:appraisal")
    public Result appraisal(@RequestBody @NotNull(message = "鉴定id不能为空") ArchiveAppraisal archiveAppraisal){
        List<String> ids = archiveAppraisal.getIds();
        List<ArchiveAppraisal> archiveAppraisals = archiveAppraisalMapper.selectBatchIds(ids);
        archiveAppraisals.forEach(i->{
            i.setState("0");
            i.setResult(archiveAppraisal.getResult());
            i.setRemark(archiveAppraisal.getRemark());
            i.setAppraisalDate(archiveAppraisal.getAppraisalDate());

            this.archiveAppraisalService.updateById(i);
        });
        return new Result();
    }
}
